package com.vhans.aapi.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vhans.core.annotation.OptLogger;
import com.vhans.core.web.controller.BaseController;
import com.vhans.core.web.model.Result;
import com.vhans.core.web.page.TableDataInfo;
import com.vhans.db.model.dto.ConditionDTO;
import com.vhans.db.model.dto.quiz.QuizDTO;
import com.vhans.db.model.vo.quiz.QuizInfoVO;
import com.vhans.db.model.vo.quiz.QuizVO;
import com.vhans.db.service.IBQuizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.vhans.core.constant.OptTypeConstant.*;

/**
 * 题目控制器
 *
 * @author vhans
 **/
@Api(tags = "题目模块")
@RestController
public class QuizController extends BaseController {

    @Autowired
    private IBQuizService quizService;

    /**
     * 查看题目列表
     *
     * @param condition 条件
     * @return {@link QuizVO} 题目列表
     */
    @ApiOperation(value = "查看题目列表")
    @SaCheckPermission("agree:quiz:list")
    @GetMapping("/admin/quiz/list")
    public TableDataInfo listQuizVO(ConditionDTO condition) {
        startPage();
        List<QuizVO> list = quizService.listQuizVO(condition);
        return getDataTable(list);
    }

    /**
     * 添加题目
     *
     * @param quiz 题目信息
     * @return {@link Result<>}
     */
    @OptLogger(value = ADD)
    @ApiOperation(value = "添加题目")
    @SaCheckPermission("agree:quiz:add")
    @PostMapping("/admin/quiz/add")
    public Result<?> addQuiz(@Validated @RequestBody QuizDTO quiz) {
        quizService.addQuiz(quiz);
        return Result.success();
    }

    /**
     * 删除题目
     *
     * @param quizIdList 题目id集合
     * @return {@link Result<>}
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除题目")
    @SaCheckPermission("agree:quiz:delete")
    @DeleteMapping("/admin/quiz/delete")
    public Result<?> deleteQuiz(@RequestBody List<Integer> quizIdList) {
        quizService.deleteQuiz(quizIdList);
        return Result.success();
    }

    /**
     * 修改题目
     *
     * @param quiz 题目信息
     * @return {@link Result<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "修改题目")
    @SaCheckPermission("agree:quiz:update")
    @PutMapping("/admin/quiz/update")
    public Result<?> updateQuiz(@Validated @RequestBody QuizDTO quiz) {
        quizService.updateQuiz(quiz);
        return Result.success();
    }

    /**
     * 编辑题目
     *
     * @param quizId 题目id
     * @return {@link Result<QuizInfoVO>} 后台题目
     */
    @ApiOperation(value = "编辑题目")
    @SaCheckPermission("agree:quiz:edit")
    @GetMapping("/admin/quiz/edit/{quizId}")
    public Result<QuizInfoVO> editQuiz(@PathVariable("quizId") Integer quizId) {
        return Result.success(quizService.editQuiz(quizId));
    }

    /**
     * 上传题目图片
     *
     * @param file 文件
     * @return {@link Result<String>} 题目图片地址
     */
    @OptLogger(value = UPLOAD)
    @ApiOperation(value = "上传题目图片")
    @ApiImplicitParam(name = "file", value = "题目图片", required = true, dataType = "MultipartFile")
    @SaCheckPermission("agree:quiz:upload")
    @PostMapping("/admin/quizImage/upload")
    public Result<String> saveQuizImages(@RequestParam("file") MultipartFile file) {
        return Result.success(quizService.saveQuizImages(file));
    }
}